﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TCG
{
    public class Game
    {
        public TCGPlayers Players { get; set; }
        public Player CurrentPlayer { get; set; }
        public TCGDeck Deck { get; set; }
        public Source[] Sources { get; set; }
        public TCGDeck LastTurn = new TCGDeck();
        public Player EnemyPlayer { get { return Players.EnemyPlayer(CurrentPlayer); } }
        public WinningConditions WinConds { get; set; }

        public Game()
        {
            InitSources();
            InitDeck();
            InitPlayers();

            Init();
        }

        public void AssignConditions(GameSettings gs)
        {
            this.Deck.Type = gs.Type;
            this.WinConds = gs.WinConds;
        }

        private void Init()
        {
 	        CurrentPlayer = Players[0];

            CurrentPlayer.Deck.ValidateCards(CurrentPlayer.Resource);
        }

        private void InitSources()
        {
            Resource mana = new Resource() { Name = "Mana" };
            Source manaSource = new Source() { Name = "Mana Source", Res = mana };
            Sources = new Source[] { manaSource }; 
        }

        private void InitPlayers()
        {
            Source mana = Sources[(int)SourceIndex.MANA];

            Players = new TCGPlayers 
                    { 
                        new Player() { 
                            Name = "Player 1",
                            Health=30, 
                            Resource=new ResourceCount(new ResourcePair(mana.Res, 4)), 
                            Source=new SourceCount(new SourcePair(mana, 2)),
                            Deck = Deck.RandomDeck(3, true) }, 
                        new Player() { Name = "Player 2", 
                            Health=30, 
                            Resource=new ResourceCount(new ResourcePair(mana.Res, 4)), 
                            Source=new SourceCount(new SourcePair(mana, 2)), 
                            Deck = Deck.RandomDeck(3, true) }
                    };
        }

        private void InitDeck()
        {
            Source mana = Sources[(int)SourceIndex.MANA];

            Deck = new TCGDeck() { 
                new Card(){ 
                    Id = 0,
                    Name="Destroyer", 
                    Description="Destroyer kills women and children", 
                    ImagePath="10.png", 
                    Acts= new List<TCG.Action>()
                    { new TCG.Action(){ Type= ActionType.DoDamage, Value=7} },
                    Cost=new ResourceCount( new ResourcePair(mana.Res, 6))
                },
                new Card(){ 
                    Id = 1,
                    Name="Builder", 
                    Description="Builds tower", 
                    ImagePath="11.png", 
                    Acts= new List<TCG.Action>()
                    { new TCG.Action(){ Type= ActionType.AddHealth, Value=6} },
                    Cost=new ResourceCount( new ResourcePair( mana.Res, 5))
                },
                new Card(){ 
                    Id = 2,
                    Name="Filler", 
                    Description="Simple Filler", 
                    ImagePath="12.png", 
                    Acts= new List<TCG.Action>()
                    { new TCG.Action(){ Type= ActionType.AddResource, Value=new ResourceCount(new ResourcePair( mana.Res, 5) )} },
                    Cost=new ResourceCount( new ResourcePair( mana.Res, 2))
                },
                new Card(){ 
                    Id = 3,
                    Name="Skip Turn", 
                    Description="Pass turn", 
                    ImagePath="13.png", 
                    Acts= new List<TCG.Action>()
                    { new TCG.Action(){ Type= ActionType.None} },
                    Cost=new ResourceCount( new ResourcePair( mana.Res, 0))
                }
            };
        }

        public bool IsPayerChanged = true;

        public void EndTurn()
        {
            foreach (KeyValuePair<Source, int> s in CurrentPlayer.Source)
            {
                CurrentPlayer.Resource[s.Key.Res] += s.Value;
            }

            CurrentPlayer = EnemyPlayer;
            IsPayerChanged = true;
            CurrentPlayer.Deck.ValidateCards(CurrentPlayer.Resource);
        }

        public Player CheckWinningConditions()
        {
            return null;
        }

        public bool TurnAction(Card curCard, int pos)
        {
            bool hasToFinishTurn = true;

            if (IsPayerChanged)
            {
                LastTurn.Clear();
                IsPayerChanged = false;
            }
            LastTurn.Add(curCard);

            if (CurrentPlayer.Deck[pos].Id != curCard.Id) 
                throw new InvalidOperationException("OPAsnostE!adynadyn!!11 Invalid position of Card in Player Deck!");
            CurrentPlayer.Deck.RemoveAt(pos);

            foreach (KeyValuePair<Resource, int> r in curCard.Cost)
            {
                CurrentPlayer.Resource[r.Key] -= r.Value;
            }


            foreach (TCG.Action a in curCard.Acts)
            {
                switch (a.Type)
                {
                    case ActionType.AddHealth:
                        CurrentPlayer.Health += (int)a.Value;
                        break;
                    case ActionType.AddResource:
                        foreach (KeyValuePair<Resource, int> r in ((ResourceCount)a.Value))
                        {
                            CurrentPlayer.Resource[r.Key] += r.Value;
                        }
                        break;
                    case ActionType.AddSource:
                        throw new NotImplementedException();
                        break;
                    case ActionType.DoDamage:
                        EnemyPlayer.Health -= (int)a.Value;
                        break;
                    case ActionType.AbaddonSource:
                        throw new NotImplementedException();
                        break;
                    case ActionType.None:
                    default:
                        break;
                }
            }

            //CurrentPlayer.CardIdList.Add(Deck.RandomCardID());
            CurrentPlayer.Deck.Add(Deck.RandomCard());
            if(!hasToFinishTurn) CurrentPlayer.Deck.ValidateCards(CurrentPlayer.Resource);

            return hasToFinishTurn;
        }
    }

    public class GameSettings
    {
        public WinningConditions WinConds;
        public DeckType Type;
    }

    public enum WinCondType
    { 
        None = 0, // Holy War!
        CollectResource = 1,
        IncreaseHealth = 2,
        DefeatEnemy = 4
    }
    public class WinningCondition
    {
        public WinCondType Type;
        public ResourceCount Resources;
        public int Health;
        public WinningCondition(WinCondType type)
        { Type = type; }
    }

    public class WinningConditions : List<WinningCondition>
    { 
    }

}
